// pages/sure-order/order.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    patid:'',
    returnrid:'',
    returndizhi:'',
    value:'',
    details:'',
    patdetails:'',
  },
  // 地址列表
  addressList:function(){
    if (this.data.id){
      wx.navigateTo({
        url: '/pages/address-list/list?id=' + this.data.id,
      })
    } else if (this.data.patid){
      wx.navigateTo({
        url: '/pages/address-list/list?patid=' + this.data.patid,
      })
    }
    
  },
  // 获取秒杀商品详情
  getdetails: function () {
    ajax('/api/mall/pay_show', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id:this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        details: res.data,
      })
      if (res.data.receipt_list){
        this.setData({
          details: res.data,
          returndizhi: res.data.receipt_list,
          returnrid: res.data.receipt_list.id
        })
      }
    })
  },
  // 获取竞拍详情
  getpatdetails: function () {
    ajax('/api/user/authion_pay_show', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id: this.data.patid
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        patdetails: res.data,
      })
      if (res.data.receipt_list) {
        this.setData({
          patdetails: res.data,
          returndizhi: res.data.receipt_list,
          returnrid: res.data.receipt_list.id
        })
      }
    })
  },
  // // 获取地址
  getdizhi:function(){
    if (this.data.returnrid == '' || this.data.returnrid == undefined){
      
    }else{
      ajax('/api/mall/receipts_show', {
        data: {
          uqid: wx.getStorageSync("uqid"),
          id: this.data.returnrid
        },
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        this.setData({
          returndizhi: res.data
        })
      })
    }
    
  },
  // 去支付
  gozhifu: function () {
    if (this.data.returnrid){
      if(this.data.id){
        ajax('/api/mall/order_pay', {
          data: {
            uqid: wx.getStorageSync("uqid"),
            id: this.data.id,
            rid: this.data.returnrid,
            remark: this.data.value
          },
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "POST",
        }, res => {
          console.log(res.data)

          if (res.error == '200') {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 3000
            })
          } else if (res.error == '0') {
            wx.requestPayment({
              timeStamp: res.data.sin_data.timeStamp,
              nonceStr: res.data.sin_data.nonceStr,
              package: res.data.sin_data.package,
              signType: res.data.sin_data.signType,
              paySign: res.data.sin_data.paySign,

              success(res) {
                console.log(res)
                wx.redirectTo({
                  url: '/pages/my-order/order?select=' + 2,
                })
              },
              fail(res) {
                console.log(res)
              }
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'success',
              duration: 3000
            })
          }

        })
      }else{
        ajax('/api/mall/auction_pay', {
          data: {
            uqid: wx.getStorageSync("uqid"),
            order_id: this.data.patid,
            rid: this.data.returnrid
          },
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "POST",
        }, res => {
          console.log(res.data)

          if (res.error == '200') {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 3000
            })
          } else if (res.error == '0') {
            wx.requestPayment({
              timeStamp: res.data.sin_data.timeStamp,
              nonceStr: res.data.sin_data.nonceStr,
              package: res.data.sin_data.package,
              signType: res.data.sin_data.signType,
              paySign: res.data.sin_data.paySign,

              success(res) {
                console.log(res)
                wx.redirectTo({
                  url: '/pages/my-order/order',
                })
              },
              fail(res) {
                console.log(res)
              }
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'success',
              duration: 3000
            })
          }

        })
      }
      
    }else{
      wx.showToast({
        title: '选择地址',
        duration: 2500,
      })
    }
    
  },
  // 获取内容
  values:function(e){
    this.setData({
      value:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.returnrid != undefined && options.id != undefined){
      this.setData({
        returnrid: options.returnrid,
        id:options.id
      })
      this.getdetails()
    }
    if (options.returnrid != undefined && options.patid != undefined) {
      this.setData({
        returnrid: options.returnrid,
        patid: options.patid
      })
      this.getpatdetails()
    }
    if (options.patid != undefined){
      this.setData({
        patid: options.patid
      })
      this.getpatdetails()
    }
    if (options.id != undefined) {
      this.setData({
        id: options.id
      })
      this.getdetails()
    }
    
    this.getdizhi()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})